//  接线：            MEGA2560
//       PIN_SCS         A0
//       PIN_DISP        A1
//       PIN_MOSI        51
//       PIN_SCK         52

// IN226模块接线
//        I2C_SCL         21
//        I2C_SDA         20

//注意:
//    IN226模块上的采样电阻更换为R005
//    其他接线按数据手册中接

#include <Fonts/FreeSansBold9pt7b.h>
#include <INA226_WE.h>
#include <JDI_MIP_Display.h>
#include <Wire.h>

JDI_MIP_Display disp(90); // 90表示旋转90度，可填 0 90 180 270

#define I2C_ADDRESS 0x40

INA226_WE ina226 = INA226_WE(I2C_ADDRESS);

void setup() {
    Serial.begin(115200);
    disp.begin();
    delay(50);
    disp.displayOn();
    disp.setTextColor(COLOR_WHITE, COLOR_BLACK);
    disp.setBackgroundColor(COLOR_WHITE);
    disp.setFont(&FreeSansBold9pt7b);
    disp.setTextSize(1);
    disp.clearScreen(); // Clear the screen
    disp.refresh();     // Actually updates the display

    Wire.begin();
    if (!ina226.init()) {
        Serial.println("Failed to init INA226. Check your wiring.");
        while (1) {
        }
    }

    ina226.setAverage(AVERAGE_128);

    ina226.setConversionTime(CONV_TIME_2116);

    ina226.setMeasureMode(CONTINUOUS);

    // ina226.setCorrectionFactor(0.95); //校准因子

    ina226.setResistorRange(0.005, 10.0); //设置电阻和量程
    ina226.waitUntilConversionCompleted();
}

// 初始化变量来存储电流、时间和电荷
unsigned long previousMillis = 0;
unsigned long interval = 1000; // 时间间隔为1秒
float totalCharge_mAh = 0;     // 总的电荷值，单位为mAh

float shuntVoltage_mV = 0.0; //采样电阻上的电压
float powerVoltage_V = 0.0;  //电源电压
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
long work_time = 0;

void loop() {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

        // 获取传感器数据
        shuntVoltage_mV = ina226.getShuntVoltage_mV();
        busVoltage_V = ina226.getBusVoltage_V();
        current_mA = ina226.getCurrent_mA();
        power_mW = ina226.getBusPower();

        // 判断功率是否大于1mW，开始工作
        if (power_mW >= 1) {
            powerVoltage_V = busVoltage_V + (shuntVoltage_mV / 1000);
            float charge_mAh = current_mA / 3600.0; // 毫安乘以秒转换为毫安时
            totalCharge_mAh += charge_mAh;
            work_time += 1;
        }

        // 格式化时分秒显示，格式为 "9:99:99"
        int hours = work_time / 3600;
        int minutes = (work_time % 3600) / 60;
        int seconds = work_time % 60;

        char timeBuffer[9];
        sprintf(timeBuffer, "%1d:%02d:%02dT", hours, minutes, seconds);
        disp.setCursor(0, 20);
        disp.setTextColor(COLOR_BLACK);
        disp.print(timeBuffer);

        // 使用 dtostrf 格式化功率显示，格式为 "00.000W"
        char powerBuffer[10];
        dtostrf(power_mW / 1000.0, 6, 3,
                powerBuffer); // 将功率 mW 转换为 W，宽度6，小数3
        disp.setCursor(0, 40);
        disp.setTextColor(COLOR_BLUE);
        disp.print(powerBuffer);
        disp.print("W");

        // 画分隔线
        disp.drawLine(72, 0, 72, 47, COLOR_BLACK);
        disp.drawLine(0, 47, 144, 47, COLOR_BLACK);

        // 使用 dtostrf 格式化电压显示，格式为 "99.999V"
        char voltageBuffer[10];
        dtostrf(powerVoltage_V, 6, 3, voltageBuffer); // 宽度6，小数3
        disp.setCursor(74, 20);
        disp.setTextColor(COLOR_BLACK);
        disp.print(voltageBuffer);
        disp.print("V");

        // 使用 dtostrf 格式化电流显示，格式为 "99.999A"
        char currentBuffer[10];
        dtostrf(current_mA / 1000.0, 6, 3,
                currentBuffer); // 将电流 mA 转换为 A，宽度6，小数3
        disp.setCursor(74, 40);
        disp.setTextColor(COLOR_BLUE);
        disp.print(currentBuffer);
        disp.print("A");

        // 使用 dtostrf 格式化容量显示，格式为 "99999.999mAh"
        char chargeBuffer[13];
        dtostrf(totalCharge_mAh, 9, 3, chargeBuffer); // 宽度9，小数3
        disp.setCursor(10, 65);
        disp.setTextColor(COLOR_BLUE);
        disp.print(chargeBuffer);
        disp.print("mAh");

        // 刷新显示
        disp.refresh();
    }
}
